package com.example.demo.service;

import com.example.demo.enums.BookStatus;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2025-05-11
 * Time: 16:02
 */
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;


    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult getListByPage(PageRequest pageRequest) {
        //获取记录总数
        Integer count=bookMapper.count();
        //获取当前页的信息
        List<BookInfo> bookInfoList=bookMapper.queryListByLimit(pageRequest);
        for (BookInfo bookInfo: bookInfoList) {
            bookInfo.setStatusCN(BookStatus.getStatusByCode(bookInfo.getStatus()).getStatus());
        }
        return new PageResult<>(count,bookInfoList,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer deleteBook(Integer bookId) {
        BookInfo bookInfo=new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(BookStatus.DELETE.getCode());
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookMapper.batchDeleteBook(ids);
    }
}
